package com.yuanthink.gwt.client.controllers;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.Window;
import com.yuanthink.gwt.client.ColumnController;
import com.yuanthink.gwt.client.actions.IAction;
import com.yuanthink.gwt.client.eventbus.EventBus;
import com.yuanthink.gwt.client.modules.user.member.MemberModule;
import com.yuanthink.gwt.client.registry.ActionRegistry;
import com.yuanthink.gwt.client.registry.IActionRegistry;
/**
 * 
 * @author 06peng
 *
 */
public class MemberController implements ColumnController {

	EventBus eventBus;

    public MemberController(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    public EventBus getEventBus() {
        return eventBus;
    }

    public void setEventBus(EventBus eventBus) {
        this.eventBus = eventBus;
    }
	
    public void build() {
        IActionRegistry actionRegistry = ActionRegistry.getInstance();

        actionRegistry.register("memberAdd", new IAction(){
            public void execute(final ModelData data) {
                MemberModule.createAsync(eventBus, new MemberModule.ModuleClient() {
                    public void onSuccess(MemberModule instance) {
                        //TODO
                    }

                    public void onUnavailable() {
                        Window.alert("Not available now, please refresh the page.");
                    }
                });
            }

            public boolean shouldAddToHistory() {
                return true;  
            }
        });
		actionRegistry.register("memberList", new IAction() {
			public void execute(final ModelData data) {
				MemberModule.createAsync(eventBus,
						new MemberModule.ModuleClient() {
							public void onSuccess(MemberModule instance) {
								instance.memberListAction.execute(data);
							}

							public void onUnavailable() {
								Window.alert("Not available now, please refresh the page.");
							}
						});
			}

			public boolean shouldAddToHistory() {
				return true;
			}
		});
        actionRegistry.register("memberUpdate", new IAction(){
            public void execute(final ModelData data) {
                MemberModule.createAsync(eventBus, new MemberModule.ModuleClient() {
                    public void onSuccess(MemberModule instance) {
                        //TODO
                    	MessageBox.alert("TEST", "Member Update!!!", null);
                    }

                    public void onUnavailable() {
                        Window.alert("Not available now, please refresh the page.");
                    }
                });
            }

            public boolean shouldAddToHistory() {
                return Boolean.TRUE;   
            }
        });
    }
    
	@Override
	public void execute() {
		build();
	}
}
